#include "../../src/mylog.hpp"
#include "bench.h"
#include <unistd.h>

void sync_bench_thread_log(size_t thread_count, size_t msg_count, size_t msglen)
{
    static int num = 1;
    std::string logger_name = "sync_bench_logger" + std::to_string(num++);

    mylog::INFO("**************************************************");
    mylog::INFO("同步日志测试: %d threads, %d messages", thread_count, msg_count);

    mylog::GlobalLoggerBuilder::ptr lbp = std::make_shared<mylog::GlobalLoggerBuilder>();
    lbp->buildLoggerName(logger_name);
    lbp->buildLoggerFormatter("%m%n");
    lbp->buildLoggerSink<mylog::FileSink>("./logs/sync.log");
    lbp->buildLoggerType(mylog::LoggerType::LOGGER_SYNC);
    lbp->build();

    mylog::bench(logger_name, thread_count, msglen, msg_count);
    mylog::INFO("**************************************************");
}

void async_bench_thread_log(size_t thread_count, size_t msg_count, size_t msglen)
{
    static int num = 1;
    std::string logger_name = "async_bench_logger" + std::to_string(num++);

    mylog::INFO("**************************************************");
    mylog::INFO("异步日志测试: %d threads, %d messages", thread_count, msg_count);

    mylog::GlobalLoggerBuilder::ptr lbp(new mylog::GlobalLoggerBuilder);
    lbp->buildLoggerName(logger_name);
    lbp->buildLoggerFormatter("%m");
    lbp->buildLoggerSink<mylog::FileSink>("./logs/async.log");
    lbp->buildLoggerType(mylog::LoggerType::LOGGER_ASYNC);
    //开启非安全异步模式
    lbp->buildEnableUnSafeAsync();
    lbp->build();

    mylog::bench(logger_name, thread_count, msglen, msg_count);
    mylog::INFO("**************************************************");
}

void bench_test()
{
    // 同步写日志
    sync_bench_thread_log(1, 1000000, 100);
    sync_bench_thread_log(5, 1000000, 100);

    // 异步日志输出，为了避免因为等待落地影响时间所以日志数量降低为小于缓冲区大小进行测试
    async_bench_thread_log(1, 1000000, 100);
    async_bench_thread_log(5, 1000000, 100);
}

int main(int argc, char *argv[])
{
    bench_test();
    return 0;
}
